interface: avoid memleaks on invalid interfaces
authorFelix Fietkau <[email protected]>
Tue, 12 Aug 2025 17:47:53 +0000 (19:47 +0200)
committerFelix Fietkau <[email protected]>
Tue, 12 Aug 2025 17:53:06 +0000 (19:53 +0200)
Free strings that were allocated with strdup in interface_alloc

Signed-off-by: Felix Fietkau <[email protected]>
config.c
interface.c
interface.h

index f752da0febed1e86bde164f638715ae2f3ca2e07..56943a3d1018c559048f02718ade72fff9d48d58 100644 (file)
--- a/config.c
+++ b/config.c
@@ -193,7 +193,7 @@ config_parse_interface(struct uci_section *s, bool alias)
 error_free_config:
        free(config);
 error:
-       free(iface);
+       interface_free(iface);
 }
 
 static void
index 2ae94aeff0bcccd41de49ed5c245ef1f90e9bd0a..3c9556a29b1baced7ee92b588e0426d9d1a4e5ce 100644 (file)
@@ -702,14 +702,9 @@ interface_cleanup(struct interface *iface)
        interface_cleanup_state(iface);
 }
 
-static void
-interface_do_free(struct interface *iface)
+void interface_free(struct interface *iface)
 {
-       interface_event(iface, IFEV_FREE);
-       interface_cleanup(iface);
        free(iface->config);
-       netifd_ubus_remove_interface(iface);
-       avl_delete(&interfaces.avl, &iface->node.avl);
        free(iface->zone);
        free(iface->jail);
        free(iface->jail_device);
@@ -717,6 +712,16 @@ interface_do_free(struct interface *iface)
        free(iface);
 }
 
+static void
+interface_do_remove(struct interface *iface)
+{
+       interface_event(iface, IFEV_FREE);
+       interface_cleanup(iface);
+       netifd_ubus_remove_interface(iface);
+       avl_delete(&interfaces.avl, &iface->node.avl);
+       interface_free(iface);
+}
+
 static void
 interface_do_reload(struct interface *iface)
 {
@@ -739,7 +744,7 @@ interface_handle_config_change(struct interface *iface)
                interface_do_reload(iface);
                break;
        case IFC_REMOVE:
-               interface_do_free(iface);
+               interface_do_remove(iface);
                return;
        }
        if (iface->autostart)
index c12a266eb55009d4d89d069f006ba8c79f4748b3..eef620cb9a6e35d29d32b2de33ae4a5ab45d6862 100644 (file)
@@ -184,6 +184,7 @@ extern struct vlist_tree interfaces;
 extern const struct uci_blob_param_list interface_attr_list;
 
 struct interface *interface_alloc(const char *name, struct blob_attr *config, bool dynamic);
+void interface_free(struct interface *iface);
 
 bool interface_add(struct interface *iface, struct blob_attr *config);
 bool interface_add_alias(struct interface *iface, struct blob_attr *config);